import Mock from 'mockjs';

// 获取本地存储中的订单列表
const getLocalOrderList = () => {
    const localOrderList = localStorage.getItem('orderList');
    console.log(localOrderList)
    return localOrderList ? JSON.parse(localOrderList) : [];
};

// 保存订单列表到本地存储
const setLocalOrderList = (orderList) => {
    localStorage.setItem('orderList', JSON.stringify(orderList));
};

// 初始化订单列表数据，如果本地存储中没有数据，则生成初始数据
let orderList = getLocalOrderList();
if (orderList.length === 0) {
    for (let i = 0; i < 5; i++) {
        orderList.push(
            Mock.mock({
                orderId: '@id',
                productList: [
                    {
                        productId: '@id',
                        productName: '@ctitle(5, 10)',
                        productPrice: '@float(100, 1000, 0, 2)',
                        productQuantity: '@integer(1, 5)'
                    },
                    {
                        productId: '@id',
                        productName: '@ctitle(5, 10)',
                        productPrice: '@float(100, 1000, 0, 2)',
                        productQuantity: '@integer(1, 5)'
                    }
                ],
                userId: '@id',
                cost: '@float(100, 1000, 0, 2)',
                address: '@county(true)',
                discount: '@float(0, 100, 0, 2)',
                shippingCost: '@float(0, 50, 0, 2)',
                note: '@csentence(10, 20)',
                paymentMethod: '@pick(["Credit Card", "PayPal", "Cash on Delivery"])',
                submitTime: '@datetime("yyyy-MM-dd HH:mm:ss")'
            })
        );
    }
    setLocalOrderList(orderList); // 初始化后保存到本地存储
}

// 获取订单列表
Mock.mock(/\/order\/list/, 'get', () => {
    orderList = getLocalOrderList(); // 每次获取列表时从本地存储读取最新数据
    return { code: 1, msg: '获取成功', result: orderList };
});

// 添加订单
Mock.mock(/\/order\/add/, 'post', (options) => {
    const { body } = options;
    const newOrder = JSON.parse(body);
    orderList.push(Mock.mock(newOrder));
    setLocalOrderList(orderList); // 添加后保存到本地存储
    return { code: 1, msg: '添加成功', result: orderList };
});

// 编辑订单
Mock.mock(/\/order\/edit/, 'post', (options) => {
    const { body } = options;
    const editedOrder = JSON.parse(body);
    const index = orderList.findIndex((ele) => ele.orderId === editedOrder.orderId);
    if (index !== -1) {
        orderList.splice(index, 1, Mock.mock(editedOrder));
        setLocalOrderList(orderList); // 编辑后保存到本地存储
    }
    return { code: 1, msg: '修改成功', result: orderList };
});

// 获取单个订单
Mock.mock(RegExp(`/order/get.*`), 'get', (options) => {
    const id = options.url.match(/\/order\/get\/(\w+)/)[1];
    const data = orderList.find((ele) => ele.orderId === id);
    return { code: 1, msg: '获取成功', result: data };
});

// 删除订单
Mock.mock(RegExp(`/order/delete.*`), 'delete', (options) => {
    const id = options.url.match(/\/order\/delete\/(\w+)/)[1];
    const index = orderList.findIndex((ele) => ele.orderId === id);
    if (index !== -1) {
        orderList.splice(index, 1);
        setLocalOrderList(orderList); // 删除后保存到本地存储
    }
    return { code: 1, msg: '删除成功' };
});
